﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ENG.PSSAirbusSoundExtender
{
  public partial class FrmSystemEdit : Form
  {
    public FrmSystemEdit()
    {
      InitializeComponent();
    }

    EButton startButton;
    private EButton StartButton
    { 
      get{
      return startButton;
    }
      set{
      startButton = value;
      txtStartRectangle.Text = startButton.Name + "(" + startButton.Rectangle.ToString() + ")";
    }
    }

    EButton stopButton;
    private EButton StopButton
    {
      get
      {
        return stopButton;
      }
      set
      {
        stopButton = value;
        txtStopRectangle.Text = stopButton.Name + "(" + stopButton.Rectangle.ToString() + ")";
      }
    }

      public void GetTo (ASystem ret)
      {
        ret.Name = txtName.Text;
        ret.IsBattery = chkIsBattery.Checked;
        ret.DefaultIsOn = chkDefaultIsOn.Checked;
        ret.StartVoice = sfbStartVoice.FileName.Length == 0 ? null : sfbStartVoice.FileName;
        ret.GoVoice = sfbGoVoice.FileName.Length == 0 ? null : sfbGoVoice.FileName;
        ret.StartButton = startButton;
        ret.MinBatteryCountToRun = (int) nudBatteryCount.Value;
        if (chkStartStopSame.Checked)
          ret.StopButton = startButton;
        else
          ret.StopButton = stopButton;
      }
      public void SetBy (ASystem value)
      {
      if (value == null)
        throw new ArgumentNullException("ASystem");

      chkDefaultIsOn.Checked = value.DefaultIsOn;
      txtName.Text = value.Name;
      chkIsBattery.Checked = value.IsBattery;
      nudBatteryCount.Enabled = value.IsBattery == false;
      StartButton = value.StartButton;
      StopButton = value.StopButton;
      chkStartStopSame.Checked = (value.StartButton == value.StopButton);
      nudBatteryCount.Value = value.MinBatteryCountToRun;
        sfbStartVoice.FileName = (value.StartVoice == null ? "": value.StartVoice);
        sfbGoVoice.FileName = (value.GoVoice == null ? "" : value.GoVoice);
    }

    private void btnStartButtonCalibrate_Click(object sender, EventArgs e)
    {
      FrmCalibrate f = new FrmCalibrate();

      if(startButton != null)
        f.ButtonName = StartButton.Name;

      Program.IsInCalibration = true;
      f.ShowDialog();
      Program.IsInCalibration = false;

      if (f.DialogResult == DialogResult.OK)
      {
        Rectangle r = f.Rectangle;
        string name = f.ButtonName;

        EButton b = new EButton(name, r);

        StartButton = b;
      }
    }

    private void chkStartStopSame_CheckedChanged(object sender, EventArgs e)
    {
      btnStopButtonCalibrate.Enabled = chkStartStopSame.Checked == false;
      txtStartRectangle_TextChanged(null, null);
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void txtStartRectangle_TextChanged(object sender, EventArgs e)
    {
      if (chkStartStopSame.Checked)
        txtStopRectangle.Text = txtStartRectangle.Text;
    }

    private void btnStopButtonCalibrate_Click(object sender, EventArgs e)
    {
      FrmCalibrate f = new FrmCalibrate();

      if (stopButton != null)
        f.ButtonName = StopButton.Name;

      Program.IsInCalibration = true;
      f.ShowDialog();
      Program.IsInCalibration = false;

      if (f.DialogResult == DialogResult.OK)
      {
        Rectangle r = f.Rectangle;
        string name = f.ButtonName;

        EButton b = new EButton(name, r);

        StopButton = b;
      }
    }

    private void chkIsBattery_CheckedChanged(object sender, EventArgs e)
    {
      if (chkIsBattery.Checked)
      {
        nudBatteryCount.Value = 0;
        nudBatteryCount.Enabled = false;
      }
      else
        nudBatteryCount.Enabled = true;
    }
  }
}
